package org.framework.server.memory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.framework.core.Configation;
import org.framework.define.bean.entity.TblSystemParam;
import org.framework.define.bean.entity.TblTaskLowest;
import org.framework.define.bean.entity.TblTaskRule;
import org.framework.define.bean.entity.TblUser;
import org.framework.define.bean.entity.TblUserLevel;
import org.framework.define.bean.entity.TblUserRule;
import org.framework.server.util.dao.IServiceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Component
public class Memory {
	
	private final Logger log = LogManager.getLogger(Memory.class);
	
	public static String SERVER_ID;
	
	public static int GLOBAL_USERID = 0;
	
	public static int GLOBAL_TASKID = 0;
	
	public static Map<String, String> SYSTEM_CONFIG = new HashMap<>();
	
	public static Map<String, TblTaskRule> RULE_MAP = new HashMap<>();
	
	public static List<TblTaskLowest> LOWEST_LIST = new ArrayList<>();
	
	public static List<TblUserRule> USERRULE_LIST = new ArrayList<>();
	
	public static List<TblUserLevel> LEVEL_LIST = new ArrayList<>();
	
	@Autowired
	protected IServiceDao serviceDao;
	
	public void init(){
		loadData();
	}
	
	
	private void loadData(){
		SERVER_ID = Configation.getSysProp("sys.serverId");
		initGlog();
		initData();
	}
	private void initGlog(){
		//加载系统参数
		List<TblSystemParam> list = serviceDao.getMapper().loadAll(TblSystemParam.class);
		for (TblSystemParam systemParam : list) {
			SYSTEM_CONFIG.put(systemParam.getParamName(), systemParam.getParamValue());
		}
		
		log.info("loadAll system params");
		//初始化userid
		String userid = (String) serviceDao.queryUniqueResult(
			"select max(user_id) from tbl_user where user_id like ?",
			new Object[]{SERVER_ID+"%"}
		);
		System.out.println("userid:"+userid);
		if(userid != null && userid.length() > SERVER_ID.length())
			GLOBAL_USERID = Integer.parseInt(userid.substring(SERVER_ID.length()));
		
		log.info("GLOB_USERID:"+GLOBAL_USERID);
	}
	
	private void initData(){
		//加载任务规则
		List<TblTaskRule> rules = serviceDao.getMapper().loadAll(TblTaskRule.class);
		if(rules !=null){
			for (TblTaskRule rule : rules) {
				RULE_MAP.put(rule.getRuleName(), rule);
			}
		}
		//加载金币最低消费规则
		LOWEST_LIST = serviceDao.queryEntityList("select * from tbl_task_lowest order by price asc", TblTaskLowest.class);
		
		USERRULE_LIST = serviceDao.queryEntityList("select * from tbl_user_rule order by type asc,level asc", TblUserRule.class);
		
		LEVEL_LIST = serviceDao.queryEntityList("select * from tbl_user_level order by level asc", TblUserLevel.class);
	}
	
	public static TblUser getSessionUser(HttpServletRequest request){
		HttpSession session = request.getSession();
		if(session == null)
			return null;
		TblUser sessionUser = (TblUser) session.getAttribute("user");
		return sessionUser;
	}
	
	public IServiceDao getServiceDao() {
		return serviceDao;
	}

	public void setServiceDao(IServiceDao serviceDao) {
		this.serviceDao = serviceDao;
	}
	
}
